package hgx.spring.cloud.lession.demo;/**
 * Created by liujun on 2018/4/20.
 */

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Administrator
 * @create 2018-04-20 10:39
 */
public class GhSdkAdapterProxy  implements InvocationHandler {
    private Class target;  //被代理类
    private Object adapterObject;  //已有实现类

    public Object getProxyInstance(Class target, Object adapterObject){
        this.target=target;
        this.adapterObject=adapterObject;
        return Proxy.newProxyInstance(this.target.getClassLoader(),new Class[]{this.target},this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class clazz=this.adapterObject.getClass();
        Method adapterMehtod=clazz.getMethod(method.getName(),method.getParameterTypes());
        if(adapterMehtod !=null ){
            return adapterMehtod.invoke(adapterObject,args);
        }
        return null;
    }
}
